﻿using sydfdl.TMS.BLL;
using sydfdl.TMS.Model;
using sydfdl.TMS.Tool;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace sydfdl.TMS.Areas.SystemData.Controllers
{
    public class CompanyController : Controller
    {
        #region 业务逻辑层

        BLLT_B_Company bllCom = new BLLT_B_Company();

        #endregion

        #region 首页

        public ActionResult Index()
        {
            return View();
        }

        #endregion

        #region 方法

        /// <summary>
        ///获取公司类型
        /// </summary>
        /// <returns></returns>
        public JsonResult GetCompanyType()
        {
            IEnumerable<T_B_Dict> items = bllCom.GetCompanyType();

            return this.Json(items, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取公司列表
        /// </summary>
        public void GetAllCompanyList()
        {
            DataTable dt = new DataTable();

            try
            {
                dt = bllCom.GetAllCompanyList();
            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }

            string str = MiniUIJson.Encode(dt);

            Response.Write(str);
        }

        public void SaveData()
        {
            string json = @"[]";

            IEnumerable<T_B_Company> add = Request.Form.Get("add") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_Company>>(Request.Form.Get("add"));

            IEnumerable<T_B_Company> edit = Request.Form.Get("edit") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_Company>>(Request.Form.Get("edit"));

            IEnumerable<T_B_Company> remove = Request.Form.Get("remove") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_Company>>(Request.Form.Get("remove"));

            if (add != null)
                add.ToList().ForEach(a =>
                {
                    a.CID = Guid.NewGuid();
                    add.Where(b => b._pid == a._id).ToList().ForEach(c =>
                  {
                      c.FID = a.CID;
                  });
                });

            string str = bllCom.SaveCompanyData(add, edit, remove);

            Response.Write(str);
        }

        #endregion

    }
}
